package com.example.base.commonView

import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.res.imageResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.base.R
import com.example.base.ui.*

/**
 * zhangjunfei  2020/11/12 19:14
 */

//通用TitleBAr
@Composable
fun TitleBar(
    isDark: Boolean = isSystemInDarkTheme(),
    isShowBack: Boolean = true,
    title: String = "标题",
    onBackClick: () -> Unit
) {

    val image = imageResource(id = R.mipmap.base_icon_back)

    //横向布局
    Surface(color = if (isDark) MaterialTheme.colors.background else Color.White) {

        Row(
            modifier = Modifier.fillMaxWidth().padding(5.dp, 0.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            //返回按钮
            if (isShowBack) {
                IconButton(onClick = onBackClick) {
                    Icon(
                        image,
                        modifier = Modifier.preferredSize(20.dp, 20.dp),
                        tint = if (isDark) white else black
                    )
                }
            }
            //文本
            CommonText(
                color = if (isDark) white else black,
                text = title,
                fontSize = 16.sp,
                fontWeight = FontWeight.Bold,
                modifier = Modifier.padding(16.dp)
            )
        }
    }
}

/**
 * 通用Text
 */
@Composable
fun CommonText(
    text: String,
    fontSize: TextUnit = 14.sp,
    color: Color = Color.Black,
    textAlign: TextAlign = TextAlign.Left,
    fontWeight: FontWeight = FontWeight.Normal,
    maxLines: Int? = null,
    modifier: Modifier = Modifier.padding(10.dp)
) {
    Text(
        text = text,
        fontSize = fontSize,
        color = color,
        modifier = modifier,
        textAlign = textAlign,
        fontWeight = fontWeight,
        maxLines = maxLines ?: Int.MAX_VALUE,
        overflow = TextOverflow.Ellipsis
    )
}

/**
 * 通用CardView
 */
@Composable
fun CommonCard(
    elevation: Dp = 0.dp,
    shape: Shape = RoundedCornerShape(5.dp),
    backgroundColor: Color = gray,
    modifier: Modifier = Modifier.fillMaxWidth().padding(16.dp, 8.dp, 16.dp, 8.dp),
    content: @Composable () -> Unit
) {
    Card(
        elevation = elevation,
        shape = shape,
        backgroundColor = backgroundColor,
        modifier = modifier,
        content = content
    )
}

/**
 * 通用主题
 */
@Composable
fun CommonTheme(content: @Composable () -> Unit) {
    ComposeStudyTheme {
        // A surface container using the 'background' color from the theme
        Surface(color = MaterialTheme.colors.background, content = content)
    }
}

/**
 * 进一步封装，把竖向布局也封装组合进来
 */
@Composable
fun CommonThemeNew(content: @Composable ColumnScope.() -> Unit) {
    ComposeStudyTheme {
        // A surface container using the 'background' color from the theme
        Surface(color = MaterialTheme.colors.background) {
            Column(modifier = Modifier.fillMaxSize(), children = content)
        }
    }
}

/**
 * 通用分割线
 */
@Composable
fun CommonDivider(isDark: Boolean = isSystemInDarkTheme()) {
    Divider(
        color = if (isDark) darkGray else gray,
        thickness = 0.5.dp,
        modifier = Modifier.padding(horizontal = 20.dp)
    )
}
